# -*- coding: utf-8 -*-

import threading


class TaskThread(threading.Thread):
    def __init__(self, *args, **kwargs):
        super(TaskThread, self).__init__(*args, **kwargs)
        self.flag = threading.Event()           # 用于暂停线程的标识
        self.flag.set()                         # 设置为True
        self.running = threading.Event()        # 用于停止线程的标识
        self.running.set()                      # 将running设置为True

    def run(self):
        while self.running.isSet():
            self.flag.wait()                    # 为True时立即返回, 为False时阻塞直到内部的标识位为True后返回

            # threadLock.acquire()                # 获得锁
            self.task_run()                     # 加锁时不允许有延迟 需要立即返回
            # threadLock.release()                # 释放锁

    def task_run(self):
        pass

    def pause(self):
        self.flag.clear()                       # 设置为False, 让线程阻塞

    def resume(self):
        self.flag.set()                         # 设置为True, 让线程停止阻塞

    def stop(self):
        self.flag.set()                         # 将线程从暂停状态恢复, 如何已经暂停的话
        self.running.clear()                    # 设置为False

# threadLock = threading.Lock()
